package com.dahaiwuliang.code.server.impl;


import com.dahaiwuliang.code.dao.ArcTypeDao;
import com.dahaiwuliang.code.pojo.ArcType;
import com.dahaiwuliang.code.run.StartupRunner;
import com.dahaiwuliang.code.server.ArcTypeService;
import com.dahaiwuliang.code.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("arcTypeService")
public class ArcTypeServiceImpl implements ArcTypeService {
    @Autowired
    private ArcTypeDao arcTypeDao;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private StartupRunner startupRunner;
    @Override
    public List<ArcType> list(Integer page, Integer pageSize, Sort.Direction direction, String... properties) {
        Page<ArcType> arcTypePage=arcTypeDao.findAll(PageRequest.of(page-1,pageSize,direction,properties));
        return arcTypePage.getContent();
    }

    @Override
    public List listAll(Sort.Direction direction, String... properties) {
       if (redisTemplate.hasKey(Consts.ALL_ARC_TYPE_NAME)){
           return redisTemplate.opsForList().range(Consts.ALL_ARC_TYPE_NAME,0,-1);
       }else {
           List list=arcTypeDao.findAll(Sort.by(direction,properties));
           if (list!=null&&list.size()>0){
               for (int i=0;i<list.size();i++){
                   redisTemplate.opsForList().rightPush(Consts.ALL_ARC_TYPE_NAME,list.get(i));
               }
           }
        return list;}
    }

    @Override
    public Long getCount() {
        return arcTypeDao.count();
    }

    @Override
    public void save(ArcType arcType) {
    boolean flag=false;
        if(arcType.getArcTypeId()==null){
            flag = true;
        }
    arcTypeDao.save(arcType);
    if (flag){ //新增类型
        redisTemplate.opsForList().rightPush(Consts.ALL_ARC_TYPE_NAME,arcType);

    }else { //新增类型

        redisTemplate.delete(Consts.ALL_ARC_TYPE_NAME);


    }
        startupRunner.loadData();
    }

    @Override
    public void delete(Integer id) {
      arcTypeDao.deleteById(id);

    }

    @Override
    public ArcType getById(Integer id) {
        return arcTypeDao.getOne(id);
    }
}
